package com.sgb.kjwl.view.ui.activity.enterprise;

import android.view.View;

import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.sgb.kjwl.AppActivity;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.ActivityDealedCasesBinding;
import com.sgb.kjwl.di.enterprise.DaggerEnterpriseComponent;
import com.sgb.kjwl.di.enterprise.EnterpriseModule;
import com.sgb.kjwl.model.entity.enterprise.TransactionCasesEntity;
import com.sgb.kjwl.view.ui.adapter.enterprise.DealedCasesDetailAdapter;
import com.sgb.kjwl.view.ui.adapter.enterprise.DealedCasesPublishAdapter;
import com.sgb.kjwl.viewmodel.enterprise.PublishedDealedCasesActivityModel;
import com.swgk.core.base.di.AppComponent;
import com.swgk.core.xrecyclerview.XRecyclerView;

import java.util.List;

import javax.inject.Inject;

public class PublishedDealedCasesActivity extends AppActivity {

    private ActivityDealedCasesBinding binding;
    @Inject
    PublishedDealedCasesActivityModel mModel;

    DealedCasesDetailAdapter dealedCasesDetailAdapter;
    long id;
    private boolean mIsLoadmore;


    @Override
    protected void setupActivityComponent(AppComponent appComponent) {
        DaggerEnterpriseComponent.builder().appComponent(appComponent)
                .enterpriseModule(new EnterpriseModule(this))
                .build()
                .inject(this);
    }

    @Override
    protected void initView() {
        super.initView();
        binding = DataBindingUtil.setContentView(this, R.layout.activity_dealed_cases);
        id = getIntent().getLongExtra("id", 0);

        if (dealedCasesDetailAdapter == null)
            dealedCasesDetailAdapter = new DealedCasesDetailAdapter(this, null);

        LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
        binding.displayView.setLayoutManager(layoutManager);
        binding.displayView.setAdapter(dealedCasesDetailAdapter);
        binding.displayView.setLoadingMoreEnabled(true);
        binding.displayView.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                mIsLoadmore = false;
                mModel.getTransactionList(PublishedDealedCasesActivity.this, false, String.valueOf(id));
            }

            @Override
            public void onLoadMore() {
                mIsLoadmore = true;
                mModel.getTransactionList(PublishedDealedCasesActivity.this, true, String.valueOf(id));

            }
        });

    }

    @Override
    protected void init() {
        id = getIntent().getLongExtra("id", 0);
        mModel.getTransactionList(this, false, String.valueOf(id));
        mModel.ldCaseList.observe(this, list -> showList(list, mIsLoadmore));
    }


    /**
     * @param list
     * @param isLoadMore
     */
    public void showList(List<TransactionCasesEntity> list, boolean isLoadMore) {
        binding.displayView.refreshComplete();
        if (isLoadMore) {
            if (null != list && list.size() > 0) {
                binding.displayView.loadMoreComplete();
                dealedCasesDetailAdapter.addDatas(list);
            } else {
                binding.displayView.setNoMore(true);
            }
            binding.displayView.setVisibility(View.VISIBLE);
        } else {
            if (list.size() == 0) {
                binding.emptyView.setVisibility(View.VISIBLE);
            } else {
                binding.emptyView.setVisibility(View.GONE);
            }
            binding.displayView.refreshComplete();
            binding.displayView.setLoadingMoreEnabled(true);
            dealedCasesDetailAdapter.setDatas(list);
        }
    }
}
